/**
 * 根据路径字符串从嵌套对象中获取值
 * @param str 路径字符串，例如 "a.b.c" 或单个键 "a"
 * @param obj 目标对象，可以是多层嵌套结构或空对象
 * @returns 路径对应的值，如果路径不存在则返回 undefined
 */
const getValueByPath = (str: string, obj: any): unknown => {
  // 空路径直接返回整个对象
  if (str === "") return undefined;

  // 分割路径字符串为键数组
  const keys = str.split(".");
  let current = obj;

  // 逐层遍历对象
  for (const key of keys) {
    if (current === null || current === undefined) return undefined;
    current = current[key];
  }

  return current;
}

export const T = (str: string) => {
  const message: { [key: string]: any } = getStorage("lang", "sessionStorage");
  const language = getStorage("user", "sessionStorage")?.language;
  if (message && language && str) {
    const useLang = message[language];
    return getValueByPath(str, useLang);
  }
  return undefined;
};